/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package query;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;
import edu.smu.tspell.wordnet.*;
/**
 *
 * @author teeh
 */
public class ExpandQuery {

    private static final String WORD_TYPES = "TIME" + File.separator + "English_Words_Types.txt";
    private WordNetDatabase database;
    private Vector<String> nouns;
    
    public ExpandQuery()
    {
    	if(System.getProperty("os.name").startsWith("Windows"))
        {
                System.setProperty("wordnet.database.dir", "C:\\Program Files\\WordNet\\2.1\\dict\\");
        }
        else
        {
                System.setProperty("wordnet.database.dir", "/usr/local/WordNet-3.0/dict/");
        }

        database = WordNetDatabase.getFileInstance();
        nouns = new Vector<String>();

        try {
            String whitStr = "^([\\s.,;:\\-\\(\\)\\!\\?'\"])+$";
            BufferedReader reader = new BufferedReader(new FileReader(WORD_TYPES));
            String line = "";

            while ((line = reader.readLine()) != null)
            {
                    if(!line.matches(whitStr) && line.length() > 0)
                    {
                            String [] tokens = line.split("[\\s.,;:\\-\\(\\)\\!\\?'\"]");
                            int i=0;

                            while(tokens[i].matches(" ")) {
                                i++;
                            }

                            if(tokens[i+1].matches("NoC") && tokens[i].length() > 1) {
                                nouns.add(tokens[i]);
                            }

                    }
            }

            reader.close();

        } catch (FileNotFoundException e) {
                e.printStackTrace();
        } catch (IOException e) {
                e.printStackTrace();
        }

    	
    }

    public String expand(String query) {


        String [] single_queries = query.split("[\\s.,;:\\-\\(\\)\\!\\?'\"]");
        
        Vector<String> synonyms = new Vector<String>();

        for(int i=0; i<single_queries.length; i++)
        {
            for(int j=0; j<nouns.size(); j++) {

                if(single_queries[i].matches(nouns.get(j).toUpperCase()))
                {
                    

                    Synset[] synsets = database.getSynsets(single_queries[i], SynsetType.NOUN);

                    String last = "";
                    
                    for (int k = 1; k < synsets.length; k++) {
                        
                        NounSynset nounSynset = (NounSynset)synsets[k];
                        
                        String[] word_forms = nounSynset.getWordForms();
                        
                        if(!word_forms[0].matches(last)) {
                            query = query + " " + word_forms[0].toUpperCase();
                            last = word_forms[0];
                        }
                    }
                }
            }
        }

        //System.out.println(query);
        return query;

    }
}
